package com.changjiang.luban.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:自定义字符串工具类
 *
 * @author changjiang
 * Date: 2018-04-02
 * Time: 14:18
 */
public class StringUtils {

    static Logger log = LoggerFactory.getLogger(StringUtils.class);


    /** SQL count函数查询前缀 */
    private static final String COUNT_QUERY_PREFIX="select count(*) ";


    /**
     * 根据sql查询转换成查询结果数的sql
     * @param sql 数据库查询语句
     * @return !="" 转换成功的SQL "" 转换失败，SQL语句问题
     */
    public static String sqlToCountSql(String sql){
        int index = sql.toLowerCase().indexOf("from");
        if(index!=-1){
            return COUNT_QUERY_PREFIX + sql.substring(index);
        }else{
            log.error("SQL语句转换失败,SQL----->"+sql);
            return "";
        }
    }

    /**
     * 将字符串拆分成数组
     * @param str 需要拆分的字符串
     * @param split 拆分字符
     * @return List<String>
     */
    public static List<String> strSplitList(String str,String split){
        String[] strings = str.split(split);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < strings.length ; i++) {
            list.add(i,strings[i]);
        }
        return list;
    }

}
